<?php

	// +----------------------------------------------------------------------
	// | Api接口公共控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\api\notify;

    use app\common\controller\Base;
    use think\Response;

	class Common extends Base
	{
		/**
		 * 应用实例
		 * @var \think\App
		 */
		protected $app;
		protected $data;
		/**
		 * Request实例
		 * @var \think\Request
		 */
		protected $request;

		/**
		 * 架构函数
		 */
		protected function initialize()
		{
			if ($this->request->isOptions()) {
				abort(200, '通过');
			}
			parent::initialize();
			$this->app = app();
			$this->request = $this->app['request'];
            //移除HTML标签
            $this->request->filter('trim,strip_tags,htmlspecialchars');

		}
//
//		/**
//		 * 空操作，调用对应方法
//		 * @author 微尘 <yicmf@qq.com>
//		 * @datetime: 2020/6/30 11:33
//		 */
//		public function _empty()
//		{
//
//			$re = $this->request->param(false);
//
//			$action = $this->request->action(true);
//			if (!empty($this->openEvent) &&
//				(in_array($action, $this->openEvent) || in_array($action, array_keys($this->openEvent)))
//			) {
//				try {
//					$keys = array_keys($this->openEvent);
//					foreach ($keys as $index => $key) {
//						if (!$key) {
//							unset($keys[$index]);
//						}
//					}
//					if (in_array($action, $keys)) {
//						$method = $this->openEvent[$action];
//						if ($this->request->isGet() && isset($method['get'])) {
//							throw new Exception($method['get']);
//						}
//						if ($this->request->isPost() && isset($method['post'])) {
//							throw new Exception($method['post']);
//						}
//						if (isset($method['action'])) {
//							$action = $method['action'];
//						}
//						if (isset($method['event'])) {
//							$this->event = new $method['event'];
//						}
//						if (isset($method['validateName'])) {
//							$this->validateName = $method['validateName'];
//						}
//					}
//					if ($this->request->isPost() && $this->validateName) {
//						$result = $this->validate($this->request->post(), $this->validateName);
//						if ($result !== true) {
//							throw new Exception('数据验证错误，【' . $result . '】');
//						}
//					}
//					$data = $this->event->$action($this->request->param(false), $this->user);
//				} catch (\Exception $e) {
//					$data['code'] = 1;
//					$data['message'] = $e->getMessage();
//				}
//			} else {
//				$data['code'] = 1;
//				$data['message'] = '当前操作未定义';
//			}
//			return json($data);
//		}
//

		/**
		 * 返回ajax请求数据
		 * @param array $data
		 * @return Response
		 */
		protected function autoAjax(array $data): Response
		{
			if ($data['code'] == 0) {
				return $this->successAjax(isset($data['data'])?$data['data']:null, $data['message'] ?? null);
			} else {
				return $this->errorAjax($data['message']);
			}
		}

		/**
		 * 返回ajax请求数据
		 * @param array|string|null $data
		 * @param string|null $message
		 * @param int $code
		 * @return Response
		 */
		protected function successAjax($data, ?string $message = 'success', int $code = 0): Response
		{
			$result['data'] = $data;
			$result['code'] = $code;
			$result['message'] = $message;
			return response_ajax($result);
		}

		/**
		 * 返回ajax请求数据
		 * @param string|array $message
		 * @param int $code
		 * @return Response
		 */
		protected function errorAjax($message = '请求错误', int $code = 1): Response
		{
			$result['code'] = $code;
			$result['message'] = $message;
			return response_ajax($result);
		}

	}
